const playBtn = document.querySelector(".play");
const seekInput = document.querySelector(".seek-input");
let starTime = document.querySelector(".start-time");
let endTime = document.querySelector(".end-time");
let musicTitle = document.querySelector(".title");
let musicImg = document.querySelector(".music-img");

let audio = new Audio(); //创建audio对象
let isPlayer = false; //播放暂停控制

audio.addEventListener("ended", function(){
    nextTrack();
})

let updateTimer = setInterval(seekUpdate, 1000);

//定义音乐列表资源
let track_list = [
    {
        name: "Night Owl",
        artist: "Broke For Free",
        image: "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2813440055,1849072238&fm=26&gp=0.jpg",
        path: "http://tyst.migu.cn/public/product08/2018/04/12/2018%E5%B9%B404%E6%9C%8812%E6%97%A515%E7%82%B938%E5%88%86%E7%B4%A7%E6%80%A5%E5%86%85%E5%AE%B9%E5%87%86%E5%85%A5SONY18%E9%A6%96/%E6%AD%8C%E6%9B%B2%E4%B8%8B%E8%BD%BD/MP3_40_16_Stero/%E6%94%B9%E5%8F%98%E8%87%AA%E5%B7%B1-%E7%8E%8B%E5%8A%9B%E5%AE%8F.mp3"
    },
    {
        name: "Enthusiast",
        artist: "Tours",
        image: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1860240789,1191019851&fm=26&gp=0.jpg",
        path: "https://m10.music.126.net/20200523095243/a06d6fb4904bc679a318b250f5a4f30e/ymusic/065c/075b/535c/1613f069310178ef64add65f40f25593.mp3"
    },
    {
        name: "Shipping Lanes",
        artist: "Chad Crouch",
        image: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590208012170&di=312f7440f1d5fb6df6c6da8c2efcaf48&imgtype=0&src=http%3A%2F%2Fimg.9ku.com%2Fgeshoutuji%2Fsingertuji%2F4%2F40945%2F40945_4.jpg",
        path: "https://m10.music.126.net/20200523095336/5c370bbd9e05866a86cafbad14e3e77c/ymusic/0359/050f/0e08/4308e63dd02e64336b829b12aec4ebb3.mp3",
    },
];
//当前音乐播放序号
let index = 0;

/**
 * 初始化audio
 * 
 */
function onLoadMusic(index) {
    audio.src = track_list[index].path; //音乐的路径
    musicTitle.innerHTML = track_list[index].name + "  " + track_list[index].artist;
    musicImg.src=track_list[index].image;
    audio.load(); //加载音乐资源
}

/**
 * 下一首
 */
function nextTrack(){
    if(++index > track_list.length-1){ //index: 1, 2 ,0
        index = 0;
    }
    onLoadMusic(index);
    onPlay();
}

/**
 * 上一首
 */
function prevTrack(){
    if(--index < 0){
        index = track_list.length-1;
    }
    onLoadMusic(index);
    onPlay();
}


/**
 * 播放与暂停
 **/
function onPlayAndPause() {
    if (isPlayer == true) {
       onPause();
    } else {
       onPlay();
    }
}

function onPlay() {
    audio.play(); //播放
    isPlayer = true;
    playBtn.innerHTML = '<span class="icon-pause"></span>';
}
function onPause() {
    audio.pause();  //暂停
    isPlayer = false;
    playBtn.innerHTML = '<span class="icon-play2 "></span>'; //显示播放状态按钮
}


//更新播放进度
function seekUpdate() {
    if (!isNaN(audio.duration) && isPlayer == true) {
        let seekTime = audio.currentTime / audio.duration * 100;
        seekInput.value = seekTime;

        //当前时间
        let currentMinutes = Math.floor(audio.currentTime / 60);
        let currentSeconds = Math.floor(audio.currentTime - currentMinutes * 60); //秒不能超过60，所以要当前总秒数 减去 分钟的总秒数 得到 剩下秒数
        if (currentMinutes < 10) currentMinutes = "0" + currentMinutes
        if (currentSeconds < 10) currentSeconds = "0" + currentSeconds
        starTime.innerHTML = currentMinutes + ":" + currentSeconds

        //总时间
        let durationMinutes = Math.floor(audio.duration / 60);
        let durationSeconds = Math.floor(audio.duration - durationMinutes * 60);

        if (durationSeconds < 10) { durationSeconds = "0" + durationSeconds; }
        if (durationMinutes < 10) { durationMinutes = "0" + durationMinutes; }
        endTime.innerHTML = durationMinutes + ":" + durationSeconds;
    }
}
// 拖动滑块
function seekTo() {
    audio.currentTime = seekInput.value / 100 * audio.duration
}
//初始化播放
onLoadMusic(index);